<template>
  <div class="search-info">
    <div class="search-header">
      <span class="span1">
        <input
          type="text"
          placeholder="请输入内容"
          v-model="title"
          @input="add"
          @keydown.enter="enter"
        />
      </span>
      <span class="sp" v-if="show" @click="enter">搜索</span>
      <span
        class="sp"
        style="font-size: 14px; color: #d2d2d1"
        @click="$router.go(-1)"
        v-else
        >取消</span
      >
    </div>
    <section class="history-search" v-show="show == false">
      <header class="search-headers">
        <p>
          <span>历史搜索</span>
          <span class="iconfont icon-shanchu" @click="sc"></span>
        </p>
      </header>
      <div class="list">
        <span
          class="item"
          v-for="(item, index) in this.$store.state.list"
          :key="index"
          @click="search(item)"
        >
          {{ item }}
          <span class="iconfont icon-quxiao" @click.stop="scs(index)"></span>
        </span>
      </div>
    </section>
    <mydetaillist
      v-for="item in data"
      :key="item.id"
      :item="item"
      v-show="data.length > 0 && show"
    ></mydetaillist>
  </div>
</template>

<script>
import { Dialog } from "vant";
import { getcourseBasis } from "../../utils/api";
import mydetaillist from "@/components/class/mydetaillist";
export default {
  name: "Search",
  data() {
    return {
      title: "",
      data: [],
      show: false,
    };
  },
  mounted() {},
  methods: {
    // 删除单个
    scs(i) {
      this.$store.commit("scs", i);
    },
    // 删除全部
    sc() {
      Dialog.confirm({
        title: "提示",
        message: "确认删除历史记录",
      })
        .then(() => {
          this.$store.commit("sc");
        })
        .catch(() => {});
    },
    // 绑定一个input框事件 当输入框有值的时候  搜索和取消会来回切换
    add() {
      if (this.title) {
        this.show = true;
      } else if (this.title === "") {
        this.show = false;
      }

      // console.log(this.title);
    },
    // 回车事件   往历史记录里面添加
    enter() {
      getcourseBasis(this.title).then((res) => {
        this.data = res.data.list
      });
      this.$store.commit("enter", this.title);
    },
    search(val) {
      this.title = val;
      this.enter();
      this.add();
      this.$store.commit("enter", this.title);
    },
  },
  components: {
    mydetaillist,
  },
};
</script>

<style lang="scss" scoped>
.search-info {
  width: 100%;
  height: 158px;
  padding: 0px 0px 40px;
  .search-header {
    width: 100%;
    height: 48px;
    border-bottom: 1px solid #eaeaea;
    display: flex;
    justify-content: center;
    align-items: center;
    .span1 {
      box-sizing: border-box;
      display: block;
      // width: 428.5px;
      flex: 6;
      height: 32px;
      padding-left: 13.5px;
      // margin-left: 5px;
      input {
        background-color: #e4e7ed;
        border-radius: 30px;
        display: block;
        width: 96%;
        height: 32px;
        padding: 1px 2px 1px 30px;
        border: 0px;
      }
    }
    .sp {
      flex: 1;
      font-size: 14px;
      color: black;
      text-align: left;
    }
  }
  .history-search {
    width: 100%;
    height: 40px;
    margin: 15px 0px;
    padding: 0 10px;
    .search-headers {
      width: 100%;
      height: 100%;
      display: block;
      p {
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        span {
          font-weight: bold;
        }
        .iconfont {
          color: #d2d2d1;
        }
      }
    }
  }
}
.list {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  justify-content: left;
  .item {
    display: inline-block;
    position: relative;
    height: 27px;
    line-height: 27px;
    background: #f7f7f7;
    margin-left: 10px;
    margin-top: 10px;
    padding: 0 16px;
    border-radius: 30px;
  }
  .iconfont {
    color: #61615c;
    font-size: 10px;
    position: absolute;
    right: -5px;
    top: -8px;
  }
}
</style>